package bankClient;

import java.net.URL;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import bankservice.endpoint.BankInterface;

public class ClientRequest extends  Thread {

	public	static BankInterface bankBranch1,bankBranch2;
	
	int testType;	
	int row;
	 
    CyclicBarrier barrier;

    ClientRequest(CyclicBarrier barrier,int type, int row)
    {
        this.barrier = barrier;
        this.testType = type;
        this.row = row;
    }
	
	public void run() {
	
		try {
 
	   	 	URL urlBranch1 = new URL("http://localhost:9876/endpoint?wsdl");	        	 
	   		URL urlBranch2 = new URL("http://localhost:9877/endpoint?wsdl");
	   	 				
	   	 	QName qname = new QName("http://endpoint.bankservice/", "BankImplService");

	   	 	Service serviceBranch1 = Service.create(urlBranch1, qname);	   	 	
	   	 	Service serviceBranch2 = Service.create(urlBranch2, qname);
	   	 	
	   	 	BankInterface bankBranch1 = serviceBranch1.getPort(BankInterface.class);
	   	 	BankInterface bankBranch2 = serviceBranch2.getPort(BankInterface.class);
	   	 	
			System.out.println(" ----" + row + " )Test Type " + testType + " Started  ----");
			System.out.println();	
			
			barrier.await();
			
			// According to testType, it invokes different methods   
			
			if (testType == 1 ){
							
				bankBranch1.transfer("01001", "02001", 100);								
				
			}else if (testType == 2 ){
								
				bankBranch2.transfer("02001", "01001", 10);
				
			}else if (testType == 3 ){
				
				bankBranch1.withdraw("01001", 1200);				
			}
			
			System.out.println("---------  Test Ended  ---------");
			System.out.println();			
					
		} catch (InterruptedException ex) {
			ex.printStackTrace();
		} catch (BrokenBarrierException ex) {
			ex.printStackTrace();
		} catch (Exception e) {			
			System.out.print("Exception :" + e );
		}							
	}
}
